package com.es;

import java.net.InetAddress;
import java.util.Date;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

/**   
* @Title: ElasticSearchHandler.java 
* @Package com.es 
* @Description: 创建索引、添加数据
* @author liusongqing 
* @Company www.viewhigh.com
* @date 2016年9月20日 上午9:56:45 
* @version V1.0   
*/
public class ElasticSearchHandler {
    public static void main(String[] args) {
        try {
            //创建客户端
            Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
           /* XContentBuilder builder = jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date())
                    .field("message", "trying out Elasticsearch").endObject();*/
            List<String> jsonData = DataFactory.getInitJsonData();

            for (int i = 0; i < jsonData.size(); i++) {
                IndexResponse response = client.prepareIndex("blog", "article").setSource(jsonData.get(i)).get();
                if (response.isCreated()) {
                   System.out.println("创建成功!");
                }
            }
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

